#include "ml307_parm.h"
#include "ml307.h"

static int j_t_ml307_factory(void* pram_node, uint16_t parm_size, void* arg);
static int j_t_ml307_check(void* pram_node, uint16_t parm_size, void* arg);
static int j_t_ml307_apply(void* pram_node, uint16_t parm_size, void* arg);

static int j_t_ml307_factory(void* pram_node, uint16_t parm_size, void* arg) {
    if (pram_node == NULL) {
        return -1;
    }
     
    ml307_cfg_t* cfg = pram_node;
     cfg->ml307_reg_sta.sp_time=1000;
    // 初始化设备状态
    cfg->ml307_status = ml_power_down;

    // 设置MQTT客户端数量
    cfg->ml307_mqttc_num = 1;  // 设置为4个客户端

    // 初始化4个MQTT客户端配置
    for (int i = 0; i < cfg->ml307_mqttc_num; i++) {
        // 基础信息
        cfg->smqtt[i].ml307_mqtt_id = i;  // ID从0开始
        cfg->smqtt[i].ssl = 0;            // 默认不启用SSL
        cfg->smqtt[i].topic_mux = 0;      // 默认不启用多主题订阅
        cfg->smqtt[i].mqttc_link=0;
        // 使用安全字符串拷贝
        snprintf(cfg->smqtt[i].txt_name, sizeof(cfg->smqtt[i].txt_name), "mqtt_client_%d", i);
        snprintf(cfg->smqtt[i].client_id, sizeof(cfg->smqtt[i].client_id), "pc%d", i);
        snprintf(cfg->smqtt[i].user_name, sizeof(cfg->smqtt[i].user_name), "admin");
        snprintf(cfg->smqtt[i].pass_word, sizeof(cfg->smqtt[i].pass_word), "admin");
        snprintf(cfg->smqtt[i].host, sizeof(cfg->smqtt[i].host),"broker.emqx.io");  // 修正了域名拼写
        cfg->smqtt[i].port = 1883;
        cfg->smqtt[i].Qos = 0;

        // 初始化订阅主题（最多3个）
        for (int j = 0; j < 3; j++) {
            memset(cfg->smqtt[i].SubTopic[j].subtopic, 0,sizeof(cfg->smqtt[i].SubTopic[j].subtopic));//清零
        }
        // 设置第一个订阅主题
        snprintf(cfg->smqtt[i].SubTopic[0].subtopic, sizeof(cfg->smqtt[i].SubTopic[0].subtopic),
                 "ddjia%d", i);

        // 设置发布主题
        snprintf(cfg->smqtt[i].PubTopic, sizeof(cfg->smqtt[i].PubTopic), "ppjia%d", i);
    }
    return 0;
}

/*检查设置*/
static int j_t_ml307_check(void* pram_node, uint16_t parm_size, void* arg) {
     //return -1;// TEST FACTORY
    if (pram_node == NULL) {
        return -1;
    }
    ml307_cfg_t* cfg = pram_node;
    for (int i = 0; i < get_ml307_mqttc_number(); i++) {
        if (cfg->smqtt[i].port < 0 || cfg->smqtt[i].port > 65535) {
            return -1;
        }
    }
    /*ok */
    return 0;
}
/**
 * @brief 应用MQTT客户端设置
 * @param pram_node 包含新配置的参数节点
 * @param parm_size 参数大小
 * @param arg 额外参数
 * @return 成功返回0，失败返回-1
 */
static int j_t_ml307_apply(void* pram_node, uint16_t parm_size, void* arg) {
    if (pram_node == NULL || parm_size < sizeof(ml307_cfg_t)) {
        return -1;
    }

    ml307_cfg_t* cfg = pram_node;
   
    // 验证MQTT客户端数量不超过最大值
    uint8_t client_num = (cfg->ml307_mqttc_num > 4) ? 4 : cfg->ml307_mqttc_num;

    // 应用每个MQTT客户端的配置
    for (int i = 0; i < client_num; i++) {
        // 使用安全拷贝函数替代strcpy
        strncpy(ml307_cfg.smqtt[i].txt_name, cfg->smqtt[i].txt_name,
                sizeof(ml307_cfg.smqtt[i].txt_name));
        strncpy(ml307_cfg.smqtt[i].client_id, cfg->smqtt[i].client_id,
                sizeof(ml307_cfg.smqtt[i].client_id));
        strncpy(ml307_cfg.smqtt[i].user_name, cfg->smqtt[i].user_name,
                sizeof(ml307_cfg.smqtt[i].user_name));
        strncpy(ml307_cfg.smqtt[i].pass_word, cfg->smqtt[i].pass_word,
                sizeof(ml307_cfg.smqtt[i].pass_word));
        strncpy(ml307_cfg.smqtt[i].host, cfg->smqtt[i].host, sizeof(ml307_cfg.smqtt[i].host));

        // 确保字符串以null结尾
        ml307_cfg.smqtt[i].txt_name[sizeof(ml307_cfg.smqtt[i].txt_name) - 1] = '\0';
        ml307_cfg.smqtt[i].client_id[sizeof(ml307_cfg.smqtt[i].client_id) - 1] = '\0';
        ml307_cfg.smqtt[i].user_name[sizeof(ml307_cfg.smqtt[i].user_name) - 1] = '\0';
        ml307_cfg.smqtt[i].pass_word[sizeof(ml307_cfg.smqtt[i].pass_word) - 1] = '\0';
        ml307_cfg.smqtt[i].host[sizeof(ml307_cfg.smqtt[i].host) - 1] = '\0';

        // 拷贝数值型参数
        ml307_cfg.smqtt[i].port = cfg->smqtt[i].port;
        ml307_cfg.smqtt[i].Qos = cfg->smqtt[i].Qos;
        ml307_cfg.smqtt[i].ssl = cfg->smqtt[i].ssl;
        ml307_cfg.smqtt[i].topic_mux = cfg->smqtt[i].topic_mux;
        ml307_cfg.smqtt[i].ml307_mqtt_id = cfg->smqtt[i].ml307_mqtt_id;

        // 拷贝订阅主题
        for (int j = 0; j < 3; j++) {
            strncpy(ml307_cfg.smqtt[i].SubTopic[j].subtopic, cfg->smqtt[i].SubTopic[j].subtopic,
                    sizeof(ml307_cfg.smqtt[i].SubTopic[j].subtopic));
            ml307_cfg.smqtt[i]
                .SubTopic[j]
                .subtopic[sizeof(ml307_cfg.smqtt[i].SubTopic[j].subtopic) - 1] = '\0';
        }

        // 拷贝发布主题
        strncpy(ml307_cfg.smqtt[i].PubTopic, cfg->smqtt[i].PubTopic,
                sizeof(ml307_cfg.smqtt[i].PubTopic));
        ml307_cfg.smqtt[i].PubTopic[sizeof(ml307_cfg.smqtt[i].PubTopic) - 1] = '\0';
    }

    // 更新客户端数量
    ml307_cfg.ml307_mqttc_num = client_num;

    // 注意：设备状态(ml307_status)不在此处更新
    return 0;
}
/**
 * @brief 设备参数数据结构
 * @note 该结构体包含了设备的所有参数和操作函数
 */
/*j_parm使用的数据, 打包当前设置参数和函数*/
const j_parm_user_data_t j_parm_t_ml307_p = {
    .parm = &ml307_cfg,
    .parm_size = sizeof(ml307_cfg_t),
    .op_factory = j_t_ml307_factory,
    .op_check = j_t_ml307_check,
    .op_apply = j_t_ml307_apply,
};